package cn.kt.common.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
public class PageObject<T> extends BasePojo  {
    private static final long serialVersionUID = -6019744166384603657L;
    /**总记录数*/
    private Integer rowCount;
    /**当前页记录*/
    private List<T> records;
    /**总页数*/
    private Integer pageCount;
    /**页面大小-每页最多显示的记录数*/
    private Integer pageSize;
    /**当前页码值*/
    private Integer pageCurrent;
    /**最大显示页码数*/
    private Integer pageListSize;
    /**页码*/
    private ArrayList<Integer> pageList = new ArrayList<>();
    /**中线标识*/
    private Integer halfPage;
//    /**下一页*/
//    private Integer nextPage;
//    /**上一页*/
//    private Integer oldPage;
    //......

    public PageObject(Integer rowCount, List<T> records, Integer pageSize, Integer pageCurrent,Integer pageListSize) {
        this.rowCount = rowCount;
        this.records = records;
        this.pageSize = pageSize;
        this.pageCurrent = pageCurrent;
        this.pageCount=rowCount/pageSize;
        if(rowCount%pageSize!=0)this.pageCount++;
        //设置页码
        if (pageCount <= pageListSize){
            for (int i = 1; i <= pageCount; i++) {
                pageList.add(i);
            }
        }else {
            halfPage = pageListSize/2;
            if (pageListSize % 2 != 0)this.halfPage++;
            //当前页码 小于 最大显示页码 的 半数 的时候
            if (pageCurrent <= halfPage){
                for (int i = 1; i <= pageListSize; i++) {
                    pageList.add(i);
                }
            }else {
                //如果 当前页码 - 显示数半数 <= 总页数 - 最大展示页数，那么这种情况下 每进一页都应该先后滚动
                if(pageCurrent-halfPage <= pageCount-pageListSize){
                    for (int i = 1+(pageCurrent-halfPage); i < pageCurrent + halfPage ; i++) {
                        pageList.add(i);
                    }
                }else {
                    for (int i = 1+(pageCount-pageListSize); i <= pageCount  ; i++) {
                        pageList.add(i);
                    }
                }
            }

        }
    }

}
